﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PMSSys
{
    
    public partial class PatientsListView : Form
    {
        public PatientsListView(Login newLoginWindow)
        {
            InitializeComponent();
            loginHolder = newLoginWindow;
            loginHolder.Hide();
            pasyentepropaylcontroller = new patientController();
            pasyentepropaylcontroller.DbConnect = loginHolder.DbProcessor;
            
            DataTable lst = pasyentepropaylcontroller.getDataTable();
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }
        private Login loginHolder;
        private bool logoutcontrol = false;
        private patientController pasyentepropaylcontroller; 
      

        private void PatientsListView_FormClosed(object sender, FormClosedEventArgs e)
        {
            loginHolder.Close();
        }

        private void logoutButton_Click(object sender, EventArgs e)
        {
            DialogResult res = uiUtility.confirmLogoutDialog();
            if (res == DialogResult.Yes)
            {
                logoutcontrol = true;
                Application.Restart();
            }
        }

        private void newPatientButton_Click(object sender, EventArgs e)
        {
            PatientView createWindow = new PatientView(string.Empty, pasyentepropaylcontroller);
            createWindow.ShowDialog();
            DataTable lst = pasyentepropaylcontroller.getDataTable();
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }

        private void openPatientButton_Click(object sender, EventArgs e)
        {
            String p_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            
            PatientView viewWindow = new PatientView(p_id, pasyentepropaylcontroller);
            viewWindow.ShowDialog();
            DataTable lst = pasyentepropaylcontroller.getDataTable();
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }

        private void PatientsListView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!logoutcontrol)
            {
                DialogResult res = uiUtility.confirmExit();
                if (res == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == String.Empty)
            {
                DataTable lst = pasyentepropaylcontroller.getDataTable();
                dataGridView1.DataSource = lst;
                dataGridView1.Columns[0].Visible = false;
            }
            else
            {
                DataTable lst = pasyentepropaylcontroller.searchTable(textBox1.Text);
                dataGridView1.DataSource = lst;
                dataGridView1.Columns[0].Visible = false;
            }

        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                openPatientButton.Enabled = false;
                removePatientButton.Enabled = false;
            }
            else
            {
                openPatientButton.Enabled = true;
                removePatientButton.Enabled = true;
            }
        }

        private void removePatientButton_Click(object sender, EventArgs e)
        {
            DataTable lst = pasyentepropaylcontroller.getDataTable();
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }

        private void PatientsListView_Load(object sender, EventArgs e)
        {
            ToolTip t1 = uiUtility.tooltips();

            t1.SetToolTip(this.openPatientButton, "Open patient profile.");
            t1.SetToolTip(this.newPatientButton, "Add new patient.");
            t1.SetToolTip(this.removePatientButton, "Remove patient");
            t1.SetToolTip(this.logoutButton, "Logout");
        }

    }
}
